
"use strict";
// Class definition

var RectifyMeasuresHandle = function () {

    var eventHandler = function () {
        $(document).off('click', '.add-measures');
        $(document).on('click', '.add-measures', setIndex);

        $(document).off('click', '.delete-btn');
        $(document).on('click', '.delete-btn', setIndex);

        $(document).off('click', '.submit-btn');
        $(document).on('click', '.submit-btn', submitForm);

        $(document).off('change', 'select.analytical-review');
        $(document).on('change', 'select.analytical-review', analyticalReview);

        $('#k_datepicker1_0').datepicker({
            format: 'yyyy-mm-dd'
        });

        $('#k_datepicker2_0').datepicker({
            format: 'yyyy-mm-dd'
        });
    };
    
    // Private functions
    var initRepeater = function () {
        $('.k-repeater').each(function(){
            $(this).repeater({
                show: function () {
                    $(this).slideDown();
                },
                // Enable the option below to have a 2-step remove button
                // hide: function (deleteElement) {
                //     $(this).slideUp(deleteElement);
                // },
                isFirstItemUndeletable: true
            });
        });

        $('.k_selectpicker').selectpicker();

        autosize($('textarea'));
    };

    var setIndex = function () {
        $('.k-repeater').each(function (i, repeater) {
            var kRepeaterItems = $(repeater).find('.k-repeater__item');
            kRepeaterItems.each(function (index, item) {
                var id = $(item).find('.k-input-icon.date .form-control').attr('id');
                var spliceIndex = id.lastIndexOf('_');
                var spliceStr = id.substr(0,spliceIndex+1);
                $(item).find('.k-input-icon.date .form-control').attr('id',spliceStr + index);
                $("#" + spliceStr + index).datepicker({
                    format: 'yyyy-mm-dd'
                });
            });
        });

        $('.k_selectpicker').selectpicker();
    };

    var submitForm = function () {
        var itemMeasures = [];
        var controlMeasures = $(".corrective-control-measures .k-repeater__item");
        processingData(controlMeasures,itemMeasures, 0);
        var preventMeasures = $(".prevent-measures .k-repeater__item");
        processingData(preventMeasures,itemMeasures, 1);
        console.log(itemMeasures);
    };

    var processingData = function ($target, itemMeasures, measuresType) {
        $target.each(function (i, item) {
            var idEmployees = '';
            var idEmployeesArr = $(item).find('>div:nth-child(2) select').val();
            idEmployeesArr.forEach(function (item, i) {
                idEmployees += i === 0 ? item : ',' + item;
            });
            itemMeasures.push({
                measuresContent: $(item).find('>div:first-child input').val(),
                idEmployees: idEmployees,
                limitTime: moment($(item).find('>div:nth-child(3) input').val()).unix(),
                measuresType: measuresType
            });
        });
        return itemMeasures;
    };

    var analyticalReview = function () {
        if ($(this).val() === '0') {
            $(".review-rejected").css('display', 'none');
            $(".approve").css('display', 'block');
        } else {
            $(".review-rejected").css('display', 'block');
            $(".approve").css('display', 'none');
        }
    };

    return {
        // public functions
        init: function() {
            initRepeater();
            eventHandler();
        }
    };
}();

jQuery(document).ready(function() {
    RectifyMeasuresHandle.init();
});